package cn.com.miao.lynx.common.core.util

import eu.bitwalker.useragentutils.DeviceType
import eu.bitwalker.useragentutils.UserAgent
import javax.servlet.http.HttpServletRequest

/**
 * UserAgentUtil
 * @title: UserAgentUtil
 * @description:
 * @author: dengmiao
 * @create: 2019-12-27 10:44
 **/
class UserAgentUtil {

    companion object {

        /**
         * 获取用户代理对象
         */
        @JvmStatic
        fun getUserAgent(request: HttpServletRequest): UserAgent {
            return UserAgent.parseUserAgentString(request.getHeader("User-Agent"))
        }

        /**
         * 获取设备类型
         */
        @JvmStatic
        fun getDeviceType(request: HttpServletRequest): DeviceType {
            return getUserAgent(request).operatingSystem.deviceType
        }

        /**
         * 是否是PC
         */
        @JvmStatic
        fun isComputer(request: HttpServletRequest): Boolean {
            return DeviceType.COMPUTER == getDeviceType(request)
        }

        /**
         * 是否是手机
         */
        @JvmStatic
        fun isMobile(request: HttpServletRequest): Boolean {
            return DeviceType.MOBILE == getDeviceType(request)
        }

        /**
         * 是否是平板
         */
        @JvmStatic
        fun isTablet(request: HttpServletRequest): Boolean {
            return DeviceType.TABLET == getDeviceType(request)
        }

        /**
         * 是否是手机或平板
         */
        @JvmStatic
        fun isMobileOrTablet(request: HttpServletRequest): Boolean {
            return DeviceType.MOBILE == getDeviceType(request) || DeviceType.TABLET == getDeviceType(request)
        }
    }
}
